package com.rst.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.rst.model.Department;

@Repository
@Transactional
public class DepartmentDAO {

	@Autowired
	private SessionFactory sessionFactory;

	public void saveDepartment(Department department) {
		sessionFactory.getCurrentSession().saveOrUpdate(department);			
	}

	public Department getDepartmentById(Long departmentId) {
	    Session session = sessionFactory.getCurrentSession();
	    Department department = (Department)session.get(Department.class, departmentId);
		return department;
	}

	@SuppressWarnings("unchecked")
	public List<Department> getDepartmentList() {
		Session session = sessionFactory.getCurrentSession();
	    String str = "FROM Department d WHERE d.status = :status  ";
	    	
	    Query query = session.createQuery(str);
	    query.setParameter("status",  Department.ACTIVE);
		return query.list();
	}
}




